﻿using System.Data.Entity;
using System.Linq;
using WebMobie.Models.Entities;
using System;

namespace WebMobie.Models
{
	public class WebMobileContext : DbContext
	{
		public WebMobileContext()
			: base("name=WebMobileDB")
		{

		}

		//protected override void OnModelCreating(DbModelBuilder modelBuilder)
		//{
		//        throw new UnintentionalCodeFirstException();
		//}

		public DbSet<User> Users { get; set; }
		public DbSet<BlogPost> BlogPosts { get; set; }
		public DbSet<Category> Categories { get; set; }
		public DbSet<Product> Products { get; set; }
        public DbSet<FeedBack> Feedbacks { get; set; }
		public DbSet<Detail> Details { get; set; }
		public DbSet<Order> Orders { get; set; }
		public DbSet<OrderDetail> OrderDetails { get; set; }
		public DbSet<ShippingInfo> ShippingInfos { get; set; }
		public DbSet<Promotion> Promotions { get; set; }
		public DbSet<Cart> Carts { get; set; }
		public DbSet<ThamSo> ThamSos { get; set; }

		public IQueryable<Product> GetAllProduct()
		{
			return Products.AsQueryable();
		}

        public IQueryable<FeedBack> GetAllFeedback()
        {
            return Feedbacks.AsQueryable();
        }

        public IQueryable<Order> getAllOrders()
        {
            return Orders.AsQueryable();
        }

        public decimal getDoanhThuTheo(int thang, int nam)
        {
            decimal result = 0;
            var orderList = (from o in getAllOrders() where o.DateCreated.Month == thang && o.DateCreated.Year == nam && o.Status == 3 select o).ToList();
            for (int i = 0; i < orderList.Count; i++)
            {
                Order iOrder = orderList.ElementAt(i);
                result += iOrder.Total;
            }
            return result;
        }

        public User getRandomUser()
        {
            var userList = Users.ToList();
            Random rd = new Random();
            int randomIndex = rd.Next(userList.Count);
            return userList.ElementAt(randomIndex);
        }
        public Product getRandomProduct()
        {
            var productList = Products.ToList();
            Random rd = new Random();
            int randomIndex = rd.Next(productList.Count);
            return productList.ElementAt(randomIndex);
        }
	}
}